<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0" />
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginFormRef"
        >
          <h1>Hello</h1>
          <h2>欢迎来到深圳</h2>
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              class="login_input"
              type="text"
              :prefix-icon="User"
              placeholder="请输入用户名"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              class="login_input"
              type="password"
              :prefix-icon="Lock"
              show-password
              placeholder="请输入密码"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              type="primary"
              size="default"
              class="login_btn"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus//icons-vue'
import { reactive, ref } from 'vue'
import useUserStore from '@/store/modules/user'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTimePeriod } from '@/utils/times'

// 获取el-form组件
const loginFormRef = ref()

// 获取路由器
const $router = useRouter()

//获取路由
const $route = useRoute()

// 用户相关的数据小仓库
const useStore = useUserStore()

// 收集输入框账号与密码的数据
const loginForm = reactive({ username: 'admin', password: '12345678' })

// 控制登录按钮加载效果
const loading = ref<boolean>(false)

// 登录的回调方法
const login = async (): Promise<void> => {
  // 保证所有表单检验通过之后才能发请求
  await loginFormRef.value.validate()
  //加载效果
  loading.value = true
  try {
    // await修饰 保证成功登录
    let result = await useStore.userLogin(loginForm)
    console.log(result)
    //登录的时候，查看路径是否有query.redirect参数，
    // 该参数保存上一次退出登录之前的页面的路由
    const redirect: string = $route.query.redirect as string
    //如果有就往query.redirect跳转回到上一次页面，没有则跳转到首页
    $router.push({ path: redirect || '/' })
    // 登录成功的提示的信息
    ElNotification({
      type: 'success',
      message: '登录成功',
      title: `HI,${getTimePeriod()}好!`,
    })

    //登录成功加载效果取消
    loading.value = false
  } catch (error) {
    console.log(error)
    //登录失败加载效果取消
    loading.value = false
    //登录失败的提示信息
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
  }
}

//自定义校验规则
const validatorUserName = (rule: any, value: string, cabllback: any) => {
  if (value.length >= 3) {
    cabllback()
  } else {
    cabllback(new Error('账号长度至少3位'))
  }
}

//表单检验需要的配置对象
const rules = {
  username: [{ tigger: 'change', validator: validatorUserName }],
  password: [
    {
      required: true,
      min: 6,
      message: '密码长度至少6位',
      tigger: 'change',
    },
    {
      required: true,
      max: 16,
      message: '密码长度最大16位',
      tigger: 'change',
    },
  ],
}
</script>

<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background-color: #1660ab;
  h1 {
    color: #1660ab;
    font-size: 30px;
  }
  h2 {
    color: #222;
    margin-bottom: 20px;
  }
  .login_form {
    position: relative;
    width: 80%;
    max-width: 400px;
    top: 30vh;
    background-color: #f9f2e0;
    padding: 25px;
    border-radius: 3px;
    box-shadow: 5px 5px 10px 0 #3e3e3e;
  }
  ::v-deep .el-input__wrapper:focus-within {
    border: 1px solid #1660ab;
  }
  .login_btn {
    width: 100%;
    background-color: #1660ab;
    border: 0 solid #fff;
    box-shadow: 0 0 0 0 inset;
  }
  .login_btn:hover {
    background-color: #196cbf;
    border: 1px solid #1a70c6;
  }
}
</style>
